#include "bsp_exmc_sdram.h"
#include "bsp_log.h"
uint8_t bsp_exmc_sdram_gpio_init(void)
{
    stc_gpio_init_t stcGpioInit;
    GPIO_Unlock();
    /************************* Set pin drive capacity *************************/
    (void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinDrv = PIN_DRV_HIGH;

    /* DMC_CKE */
    (void)GPIO_Init(DMC_CKE_PORT, DMC_CKE_PIN, &stcGpioInit);

    /* DMC_CLK */
    (void)GPIO_Init(DMC_CLK_PORT, DMC_CLK_PIN, &stcGpioInit);

    /* DMC_LDQM && DMC_UDQM */
    (void)GPIO_Init(DMC_DQM0_PORT, DMC_DQM0_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DQM1_PORT, DMC_DQM1_PIN, &stcGpioInit);

    /* DMC_BA[0:1] */
    (void)GPIO_Init(DMC_BA0_PORT, DMC_BA0_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_BA1_PORT, DMC_BA1_PIN, &stcGpioInit);

    /* DMC_CAS && DMC_RAS */
    (void)GPIO_Init(DMC_CAS_PORT, DMC_CAS_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_RAS_PORT, DMC_RAS_PIN, &stcGpioInit);

    /* DMC_WE */
    (void)GPIO_Init(DMC_WE_PORT, DMC_WE_PIN, &stcGpioInit);

    /* DMC_DATA[0:15] */
    (void)GPIO_Init(DMC_DATA0_PORT, DMC_DATA0_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA1_PORT, DMC_DATA1_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA2_PORT, DMC_DATA2_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA3_PORT, DMC_DATA3_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA4_PORT, DMC_DATA4_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA5_PORT, DMC_DATA5_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA6_PORT, DMC_DATA6_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA7_PORT, DMC_DATA7_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA8_PORT, DMC_DATA8_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA9_PORT, DMC_DATA9_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA10_PORT, DMC_DATA10_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA11_PORT, DMC_DATA11_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA12_PORT, DMC_DATA12_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA13_PORT, DMC_DATA13_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA14_PORT, DMC_DATA14_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_DATA15_PORT, DMC_DATA15_PIN, &stcGpioInit);

    /* DMC_ADD[0:11]*/
    (void)GPIO_Init(DMC_ADD0_PORT, DMC_ADD0_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD1_PORT, DMC_ADD1_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD2_PORT, DMC_ADD2_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD3_PORT, DMC_ADD3_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD4_PORT, DMC_ADD4_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD5_PORT, DMC_ADD5_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD6_PORT, DMC_ADD6_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD7_PORT, DMC_ADD7_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD8_PORT, DMC_ADD8_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD9_PORT, DMC_ADD9_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD10_PORT, DMC_ADD10_PIN, &stcGpioInit);
    (void)GPIO_Init(DMC_ADD11_PORT, DMC_ADD11_PIN, &stcGpioInit);

    /************************** Set EXMC pin function *************************/
    /* DMC_CKE */
    GPIO_SetFunc(DMC_CKE_PORT, DMC_CKE_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_CLK */
    GPIO_SetFunc(DMC_CLK_PORT, DMC_CLK_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_LDQM && DMC_UDQM */
    GPIO_SetFunc(DMC_DQM0_PORT, DMC_DQM0_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DQM1_PORT, DMC_DQM1_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_BA[0:1] */
    GPIO_SetFunc(DMC_BA0_PORT, DMC_BA0_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_BA1_PORT, DMC_BA1_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_CS */
    GPIO_SetFunc(DMC_CS1_PORT, DMC_CS1_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_CAS && DMC_RAS */
    GPIO_SetFunc(DMC_CAS_PORT, DMC_CAS_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_RAS_PORT, DMC_RAS_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_WE */
    GPIO_SetFunc(DMC_WE_PORT, DMC_WE_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_DATA[0:15] */
    GPIO_SetFunc(DMC_DATA0_PORT, DMC_DATA0_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA1_PORT, DMC_DATA1_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA2_PORT, DMC_DATA2_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA3_PORT, DMC_DATA3_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA4_PORT, DMC_DATA4_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA5_PORT, DMC_DATA5_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA6_PORT, DMC_DATA6_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA7_PORT, DMC_DATA7_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA8_PORT, DMC_DATA8_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA9_PORT, DMC_DATA9_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA10_PORT, DMC_DATA10_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA11_PORT, DMC_DATA11_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA12_PORT, DMC_DATA12_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA13_PORT, DMC_DATA13_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA14_PORT, DMC_DATA14_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_DATA15_PORT, DMC_DATA15_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);

    /* DMC_ADD[0:11]*/
    GPIO_SetFunc(DMC_ADD0_PORT, DMC_ADD0_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD1_PORT, DMC_ADD1_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD2_PORT, DMC_ADD2_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD3_PORT, DMC_ADD3_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD4_PORT, DMC_ADD4_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD5_PORT, DMC_ADD5_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD6_PORT, DMC_ADD6_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD7_PORT, DMC_ADD7_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD8_PORT, DMC_ADD8_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD9_PORT, DMC_ADD9_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD10_PORT, DMC_ADD10_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(DMC_ADD11_PORT, DMC_ADD11_PIN, GPIO_FUNC_12_EXMC, PIN_SUBFUNC_DISABLE);
    GPIO_Lock();
    return Ok;
}
static void EV_EXMC_DMC_InitSequence(uint32_t u32Chip,
                                     uint32_t u32Bank,
                                     uint32_t u32MdRegVal)
{
    /* SDRAM initialization sequence:
       CMD NOP->PrechargeAll->AutoRefresh->AutoRefresh->MdRegConfig->NOP */
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_NOP, 0UL);
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_PRECHARGEALL, 0UL);
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_AUTOREFRESH, 0UL);
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_AUTOREFRESH, 0UL);
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_MDREGCONFIG, u32MdRegVal);
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_NOP, 0UL);
}
uint8_t bsp_exmc_sdram_init(void)
{
    uint32_t tick;
    uint32_t u32MdRegVal;
    stc_exmc_dmc_init_t stcDmcInit;
    stc_exmc_dmc_cs_cfg_t stcCsCfg;
    PWC_Unlock(PWC_UNLOCK_CODE_0 | PWC_UNLOCK_CODE_1 | PWC_UNLOCK_CODE_2);
    /* Enable DMC module clk */
    PWC_Fcg3PeriphClockCmd(PWC_FCG3_DMC, Enable);
    /* Enable DMC. */
    EXMC_DMC_Cmd(Enable);
    PWC_Lock(PWC_UNLOCK_CODE_0 | PWC_UNLOCK_CODE_1 | PWC_UNLOCK_CODE_2);
    bsp_exmc_sdram_gpio_init();
    /* Configure DMC width && refresh period & chip & timing. */
    EXMC_DMC_StructInit(&stcDmcInit);
    stcDmcInit.u32RefreshPeriod = 900UL;
    stcDmcInit.stcChipCfg.u32ColumnBitsNumber = EXMC_DMC_COLUMN_BITS_NUM_8;
    stcDmcInit.stcChipCfg.u32RowBitsNumber = EXMC_DMC_ROW_BITS_NUM_12;
    stcDmcInit.stcChipCfg.u32MemBurst = EXMC_DMC_MEM_BURST_1;
    stcDmcInit.stcChipCfg.u32AutoRefreshChips = EXMC_DMC_AUTO_REFRESH_CHIPS_01;

    stcDmcInit.stcTimingCfg.u32CASL = 2UL;
    stcDmcInit.stcTimingCfg.u32DQSS = 0UL;
    stcDmcInit.stcTimingCfg.u32MRD = 2UL;
    stcDmcInit.stcTimingCfg.u32RAS = 3UL;
    stcDmcInit.stcTimingCfg.u32RC = 4UL;
    stcDmcInit.stcTimingCfg.u32RCD = 1UL;
    stcDmcInit.stcTimingCfg.u32RFC = 4UL;
    stcDmcInit.stcTimingCfg.u32RP = 1UL;
    stcDmcInit.stcTimingCfg.u32RRD = 1UL;
    stcDmcInit.stcTimingCfg.u32WR = 2UL;
    stcDmcInit.stcTimingCfg.u32WTR = 1UL;
    stcDmcInit.stcTimingCfg.u32XP = 1UL;
    stcDmcInit.stcTimingCfg.u32XSR = 5UL;
    stcDmcInit.stcTimingCfg.u32ESR = 5UL;
    EXMC_DMC_Init(&stcDmcInit);
     /* Configure DMC address space. */
    stcCsCfg.u32AddrMask = EXMC_DMC_ADDR_MASK_16MB;
    stcCsCfg.u32AddrMatch = EXMC_DMC_ADDR_MATCH_0X80000000;
    stcCsCfg.u32AddrDecodeMode = EXMC_DMC_CS_DECODE_ROWBANKCOL;
    EXMC_DMC_CsConfig(EXMC_DMC_CHIP_1, &stcCsCfg);
    
    /* SDRAM initialization sequence. */
    u32MdRegVal = 0;
    
    if (2UL == stcDmcInit.stcTimingCfg.u32CASL)
    {
        u32MdRegVal |= 2<<4;
    }
    else
    {
        u32MdRegVal |= 3<<4;
    }
     if (EXMC_DMC_MEM_BURST_1 == stcDmcInit.stcChipCfg.u32MemBurst)
    {
        u32MdRegVal |= 0;
    }
    else if (EXMC_DMC_MEM_BURST_2 == stcDmcInit.stcChipCfg.u32MemBurst)
    {
        u32MdRegVal |= 1;
    }
    else if (EXMC_DMC_MEM_BURST_4 == stcDmcInit.stcChipCfg.u32MemBurst)
    {
        u32MdRegVal |= 2;
    }
    else
    {
        u32MdRegVal |= 3;
    }
    
    EV_EXMC_DMC_InitSequence(EXMC_DMC_CHIP_1,EXMC_DMC_BANK_0,u32MdRegVal);
     /* Switch state from configure to ready */
    EXMC_DMC_SetState(EXMC_DMC_CTL_STATE_GO);
    EXMC_DMC_SetState(EXMC_DMC_CTL_STATE_WAKEUP);
    EXMC_DMC_SetState(EXMC_DMC_CTL_STATE_GO);

    /* Check status */
    tick = SysTick_GetTick();
    while (EXMC_DMC_CURR_STATUS_READY != EXMC_DMC_GetStatus())
    {
        if(SysTick_GetTick()-tick > 10)
        {
            return ErrorTimeout;
            LOGERR("%d Timer out------SDRAM Not ready!!!!",ErrorTimeout);
        }
    }
    return Ok;   
}
void bsp_sdram_test(void)
{
    
    for(int i=0;i<100;i++)
    {
        
    }
}    









